package com.hlx.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlx.entity.admin.RailwayAuthority;
import com.hlx.entity.admin.Train;
import com.hlx.service.admin.TrainService;
import com.hlx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/train")
public class TrainController {
    @Autowired
    private TrainService trainService;

    @GetMapping("/getAllTrain")
    public Result<List<Train>> getAllTrain(){
        return Result.success(trainService.list());
    }

    @GetMapping("/getAllTrainByPage/{pageSize}/{currentPage}")
    public Result<Page> getAllTrainByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage){
        //        分页构造
        Page page = new Page(currentPage,pageSize);
        return Result.success(trainService.page(page));
    }

    @GetMapping("/getTrainByPage/{pageSize}/{currentPage}/{search}")
    public Result<Page> getTrainByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage,@PathVariable String search){
        //        分页构造
        LambdaQueryWrapper<Train> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Train::getTrainName,search);
        Page page = new Page(currentPage,pageSize);
        return Result.success(trainService.page(page,queryWrapper));
    }

    @PostMapping("/addTrain")
    public Result<String> addTrain(@RequestBody Train train){
        if(trainService.save(train)){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @PutMapping("/putTrain")
    public Result<String> putTrain(@RequestBody Train train){
        if(trainService.updateById(train)){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @DeleteMapping ("/deleteTrain/{id}")
    public Result<String> deleteTrain(@PathVariable Integer id){
        if(trainService.removeById(id)){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
